package com.bkgy.modular.calsidata.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.calsidata.entity.JTbEdge;
import com.bkgy.modular.calsidata.entity.TbEdge;
import com.bkgy.modular.calsidata.entity.TbFalt;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TbEdgeService {

    @Resource
    private MongoTemplate mongoTemplate;//注入自带的查询工具

    /**
     * 分页查看记录
     *
     * @param data 分页参数字符串
     */
    public ResultData findDataTbEdgeByPage(String data) {
        try {
            JTbEdge jTbEdge = JSON.parseObject(data, JTbEdge.class);
            JSONObject jsonObject = null;
            Integer pageIndex = jTbEdge.getPageIndex();
            Integer pageSize = jTbEdge.getPageSize();
            Query query = new Query();
            if (null == pageIndex || null == pageSize) {
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }

            if (null != jTbEdge.getCondition()) {
                jsonObject = JSON.parseObject(jTbEdge.getCondition().toString());
            }

            String extid = null;
            if (!StringUtils.isEmpty(jsonObject.get("extid"))) {
                extid = jsonObject.get("extid").toString();
                query.addCriteria(Criteria.where("extid").regex(extid));
            }

            List<TbEdge> list = mongoTemplate.find(query.skip((long) (pageIndex - 1) * pageSize).limit(pageSize), TbEdge.class);
            Integer count = Math.toIntExact(mongoTemplate.count(query, TbEdge.class));
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    public ResultData findTbfaltData(JSONObject jsonObject) {
        String extid = jsonObject.getString("extid");
        // 创建query查询条件,根据extid(卷号)进行查询
        Query query = new Query(Criteria.where("extid").is(extid));
        TbFalt tbFalt = mongoTemplate.findOne(query, TbFalt.class);

        if (tbFalt != null) {
            Map<String, Object> edgeList = tbFalt.getEdgeList();
            Double c622_tcm_s1_bot_bur_dia = Double.parseDouble(edgeList.get("c622_tcm_s1_bot_bur_dia").toString());
            edgeList.remove("c622_tcm_s1_bot_bur_dia");
            edgeList.put("c622_tcm_s1_bot_bur_dia",c622_tcm_s1_bot_bur_dia);

            Double c622_tcm_s1_bot_mr_dia = Double.parseDouble(edgeList.get("c622_tcm_s1_bot_mr_dia").toString());
            edgeList.remove("c622_tcm_s1_bot_mr_dia");
            edgeList.put("c622_tcm_s1_bot_mr_dia",c622_tcm_s1_bot_mr_dia);

            Double c622_tcm_s1_bot_wr_dia = Double.parseDouble(edgeList.get("c622_tcm_s1_bot_wr_dia").toString());
            edgeList.remove("c622_tcm_s1_bot_wr_dia");
            edgeList.put("c622_tcm_s1_bot_wr_dia",c622_tcm_s1_bot_wr_dia);

            Double c622_tcm_s1_top_bur_dia = Double.parseDouble(edgeList.get("c622_tcm_s1_top_bur_dia").toString());
            edgeList.remove("c622_tcm_s1_top_bur_dia");
            edgeList.put("c622_tcm_s1_top_bur_dia",c622_tcm_s1_top_bur_dia);

            Double c622_tcm_s1_top_mr_dia = Double.parseDouble(edgeList.get("c622_tcm_s1_top_mr_dia").toString());
            edgeList.remove("c622_tcm_s1_top_mr_dia");
            edgeList.put("c622_tcm_s1_top_mr_dia",c622_tcm_s1_top_mr_dia);

            Double c622_tcm_s3_top_bur_dia = Double.parseDouble(edgeList.get("c622_tcm_s3_top_bur_dia").toString());
            edgeList.remove("c622_tcm_s3_top_bur_dia");
            edgeList.put("c622_tcm_s3_top_bur_dia",c622_tcm_s3_top_bur_dia);

            Double c622_tcm_s1_top_wr_dia = Double.parseDouble(edgeList.get("c622_tcm_s1_top_wr_dia").toString());
            edgeList.remove("c622_tcm_s1_top_wr_dia");
            edgeList.put("c622_tcm_s1_top_wr_dia",c622_tcm_s1_top_wr_dia);

            Double c622_tcm_s2_bot_bur_dia = Double.parseDouble(edgeList.get("c622_tcm_s2_bot_bur_dia").toString());
            edgeList.remove("c622_tcm_s2_bot_bur_dia");
            edgeList.put("c622_tcm_s2_bot_bur_dia",c622_tcm_s2_bot_bur_dia);

            Double c622_tcm_s2_bot_mr_dia = Double.parseDouble(edgeList.get("c622_tcm_s2_bot_mr_dia").toString());
            edgeList.remove("c622_tcm_s2_bot_mr_dia");
            edgeList.put("c622_tcm_s2_bot_mr_dia",c622_tcm_s2_bot_mr_dia);

            Double c622_tcm_s2_bot_wr_dia = Double.parseDouble(edgeList.get("c622_tcm_s2_bot_wr_dia").toString());
            edgeList.remove("c622_tcm_s2_bot_wr_dia");
            edgeList.put("c622_tcm_s2_bot_wr_dia",c622_tcm_s2_bot_wr_dia);

            Double c622_tcm_s2_top_bur_dia = Double.parseDouble(edgeList.get("c622_tcm_s2_top_bur_dia").toString());
            edgeList.remove("c622_tcm_s2_top_bur_dia");
            edgeList.put("c622_tcm_s2_top_bur_dia",c622_tcm_s2_top_bur_dia);

            Double c622_tcm_s2_top_mr_dia = Double.parseDouble(edgeList.get("c622_tcm_s2_top_mr_dia").toString());
            edgeList.remove("c622_tcm_s2_top_mr_dia");
            edgeList.put("c622_tcm_s2_top_mr_dia",c622_tcm_s2_top_mr_dia);

            Double c622_tcm_s2_top_wr_dia = Double.parseDouble(edgeList.get("c622_tcm_s2_top_wr_dia").toString());
            edgeList.remove("c622_tcm_s2_top_wr_dia");
            edgeList.put("c622_tcm_s2_top_wr_dia",c622_tcm_s2_top_wr_dia);

            Double c622_tcm_s3_bot_bur_dia = Double.parseDouble(edgeList.get("c622_tcm_s3_bot_bur_dia").toString());
            edgeList.remove("c622_tcm_s3_bot_bur_dia");
            edgeList.put("c622_tcm_s3_bot_bur_dia",c622_tcm_s3_bot_bur_dia);

            Double c622_tcm_s3_bot_mr_dia = Double.parseDouble(edgeList.get("c622_tcm_s3_bot_mr_dia").toString());
            edgeList.remove("c622_tcm_s3_bot_mr_dia");
            edgeList.put("c622_tcm_s3_bot_mr_dia",c622_tcm_s3_bot_mr_dia);

            Double c622_tcm_s3_bot_wr_dia = Double.parseDouble(edgeList.get("c622_tcm_s3_bot_wr_dia").toString());
            edgeList.remove("c622_tcm_s3_bot_wr_dia");
            edgeList.put("c622_tcm_s3_bot_wr_dia",c622_tcm_s3_bot_wr_dia);

            Double c622_tcm_s3_top_mr_dia = Double.parseDouble(edgeList.get("c622_tcm_s3_top_mr_dia").toString());
            edgeList.remove("c622_tcm_s3_top_mr_dia");
            edgeList.put("c622_tcm_s3_top_mr_dia",c622_tcm_s3_top_mr_dia);

            Double c622_tcm_s3_top_wr_dia = Double.parseDouble(edgeList.get("c622_tcm_s3_top_wr_dia").toString());
            edgeList.remove("c622_tcm_s3_top_wr_dia");
            edgeList.put("c622_tcm_s3_top_wr_dia",c622_tcm_s3_top_wr_dia);

            Double c622_tcm_s4_bot_bur_dia = Double.parseDouble(edgeList.get("c622_tcm_s4_bot_bur_dia").toString());
            edgeList.remove("c622_tcm_s4_bot_bur_dia");
            edgeList.put("c622_tcm_s4_bot_bur_dia",c622_tcm_s4_bot_bur_dia);

            Double c622_tcm_s4_bot_mr_dia = Double.parseDouble(edgeList.get("c622_tcm_s4_bot_mr_dia").toString());
            edgeList.remove("c622_tcm_s4_bot_mr_dia");
            edgeList.put("c622_tcm_s4_bot_mr_dia",c622_tcm_s4_bot_mr_dia);

            Double c622_tcm_s4_bot_wr_dia = Double.parseDouble(edgeList.get("c622_tcm_s4_bot_wr_dia").toString());
            edgeList.remove("c622_tcm_s4_bot_wr_dia");
            edgeList.put("c622_tcm_s4_bot_wr_dia",c622_tcm_s4_bot_wr_dia);

            Double c622_tcm_s4_top_bur_dia = Double.parseDouble(edgeList.get("c622_tcm_s4_top_bur_dia").toString());
            edgeList.remove("c622_tcm_s4_top_bur_dia");
            edgeList.put("c622_tcm_s4_top_bur_dia",c622_tcm_s4_top_bur_dia);

            Double c622_tcm_s4_top_mr_dia = Double.parseDouble(edgeList.get("c622_tcm_s4_top_mr_dia").toString());
            edgeList.remove("c622_tcm_s4_top_mr_dia");
            edgeList.put("c622_tcm_s4_top_mr_dia",c622_tcm_s4_top_mr_dia);

            Double c622_tcm_s4_top_wr_dia = Double.parseDouble(edgeList.get("c622_tcm_s4_top_wr_dia").toString());
            edgeList.remove("c622_tcm_s4_top_wr_dia");
            edgeList.put("c622_tcm_s4_top_wr_dia",c622_tcm_s4_top_wr_dia);

            Double c622_tcm_s5_bot_bur_dia = Double.parseDouble(edgeList.get("c622_tcm_s5_bot_bur_dia").toString());
            edgeList.remove("c622_tcm_s5_bot_bur_dia");
            edgeList.put("c622_tcm_s5_bot_bur_dia",c622_tcm_s5_bot_bur_dia);

            Double c622_tcm_s5_bot_mr_dia = Double.parseDouble(edgeList.get("c622_tcm_s5_bot_mr_dia").toString());
            edgeList.remove("c622_tcm_s5_bot_mr_dia");
            edgeList.put("c622_tcm_s5_bot_mr_dia",c622_tcm_s5_bot_mr_dia);

            Double c622_tcm_s5_bot_wr_dia = Double.parseDouble(edgeList.get("c622_tcm_s5_bot_wr_dia").toString());
            edgeList.remove("c622_tcm_s5_bot_wr_dia");
            edgeList.put("c622_tcm_s5_bot_wr_dia",c622_tcm_s5_bot_wr_dia);

            Double c622_tcm_s5_top_bur_dia = Double.parseDouble(edgeList.get("c622_tcm_s5_top_bur_dia").toString());
            edgeList.remove("c622_tcm_s5_top_bur_dia");
            edgeList.put("c622_tcm_s5_top_bur_dia",c622_tcm_s5_top_bur_dia);

            Double c622_tcm_s5_top_mr_dia = Double.parseDouble(edgeList.get("c622_tcm_s5_top_mr_dia").toString());
            edgeList.remove("c622_tcm_s5_top_mr_dia");
            edgeList.put("c622_tcm_s5_top_mr_dia",c622_tcm_s5_top_mr_dia);

            Double c622_tcm_s5_top_wr_dia = Double.parseDouble(edgeList.get("c622_tcm_s5_top_wr_dia").toString());
            edgeList.remove("c622_tcm_s5_top_wr_dia");
            edgeList.put("c622_tcm_s5_top_wr_dia",c622_tcm_s5_top_wr_dia);

            // 查询到的数据外层套mongdb,mongxian
            Map<String,Object> map = new HashMap<>();
            map.put("mongdian",edgeList);

            Map<String,Map<String,Object>> list = new HashMap<>();
            list.put("mongdb",map);
            return ResultData.ResultDataSuccess(list);
        } else {
            return ResultData.ResultDataFaultSelf("查询数据为空",null);
        }

    }
}
